from transformers import pipeline

pipe = pipeline(
    task="sentiment-analysis",
    model=r"D:\ideaSpace\MyPython\models\bert-base-chinese",
    device="cpu"
)

result = pipe("今儿上海可真冷啊")
print("sentiment-analysis：", result)

classifier = pipeline(
    task="ner",
    # model=r"D:\ideaSpace\MyPython\models\bert-base-chinese", # 通用性强，需微调
    model=r"D:\ideaSpace\MyPython\models\Ernie-3.0-base-chinese-finetuned-ner",
    device="cpu"
)

result = classifier("Hugging Face is a French company based in New York City.")
print("ner：", result)

question_answerer = pipeline(
    task="question-answering",
    model=r"D:\ideaSpace\MyPython\models\tinybert-6l-768d-squad2",
    device="cpu"
)

result = question_answerer(question="What is the name of the repository?", context="The name of the repository is huggingface/transformers")
print("question-answering：", result)

summarizer = pipeline(task="summarization",
                      model=r"D:\ideaSpace\MyPython\models\distilbart-cnn-12-6",
                      min_length=8,
                      max_length=32)
# 下载的模型文件夹下必须包含merges.txt，否则会报TypeError: expected str, bytes or os.PathLike object, not NoneType
# 这个错误表明模型的分词器（Tokenizer）未能正确加载 merges.txt 文件，导致 NoneType 错误
result = summarizer("In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, "
                    "replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. "
                    "For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. "
                    "On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. "
                    "In the former task our best model outperforms even all previously reported ensembles.")

print("summarization：", result)


classifier = pipeline(task="audio-classification", model=r"D:\ideaSpace\MyPython\models\whisper-small")
# result = classifier("https://hf-mirror.com/datasets/Narsil/asr_dummy/tree/main/mlk.flac") # 会报ValueError: Malformed soundfile
result = classifier("D:/ideaSpace/MyPython/datasets/mlk.flac")
print("audio-classification：", result)

transcriber = pipeline(task="automatic-speech-recognition", model=r"D:\ideaSpace\MyPython\models\whisper-tiny")
text = transcriber("D:/ideaSpace/MyPython/datasets/mlk.flac")
print("automatic-speech-recognition：", text)


classifier = pipeline(task="image-classification", model=r"D:\ideaSpace\MyPython\models\efficientnet-b3")
# 直接使用url会报PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000015F5DBF05E0>
# result = classifier("https://hf-mirror.com/datasets/huggingface/documentation-images/tree/main/pipeline-cat-chonk.jpeg")
result = classifier("D:/ideaSpace/MyPython/datasets/pipeline-cat-chonk.jpeg")
print("image-classification：", result)

# 使用detr-resnet-50模型或detr-resnet-50-panoptic模型都会去下载resnet50.a1_in1k模型的权重，哪怕先手动把resnet50.a1_in1k模型权重下载到本地也没用
detector = pipeline(task="object-detection", model=r"D:\ideaSpace\MyPython\models\yolos-tiny")
result = detector("D:/ideaSpace/MyPython/datasets/pipeline-cat-chonk.jpeg")
print("object-detection: ", result)